{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "e2ba1ef5",
   "metadata": {},
   "outputs": [],
   "source": [
    "#导入pandas和numpy 库\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 读取数据集\n",
    "user_artists = pd.read_csv('./data/music/user_artists.dat',sep='\\t')\n",
    "artists = pd.read_csv('./data/music/artists.dat', sep='\\t', usecols=['id','name'])\n",
    "#去除不必要的列\n",
    "#user artists =user_artists.drop(['weight'],acis=1)# 删除user-artists 中的weight 列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "faf3385e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>userID</th>\n",
       "      <th>artistID</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>51</td>\n",
       "      <td>13883</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>52</td>\n",
       "      <td>11690</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>53</td>\n",
       "      <td>11351</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>54</td>\n",
       "      <td>10300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>55</td>\n",
       "      <td>8983</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92829</th>\n",
       "      <td>2100</td>\n",
       "      <td>18726</td>\n",
       "      <td>337</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92830</th>\n",
       "      <td>2100</td>\n",
       "      <td>18727</td>\n",
       "      <td>297</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92831</th>\n",
       "      <td>2100</td>\n",
       "      <td>18728</td>\n",
       "      <td>281</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92832</th>\n",
       "      <td>2100</td>\n",
       "      <td>18729</td>\n",
       "      <td>280</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92833</th>\n",
       "      <td>2100</td>\n",
       "      <td>18730</td>\n",
       "      <td>263</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>92834 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       userID  artistID  weight\n",
       "0           2        51   13883\n",
       "1           2        52   11690\n",
       "2           2        53   11351\n",
       "3           2        54   10300\n",
       "4           2        55    8983\n",
       "...       ...       ...     ...\n",
       "92829    2100     18726     337\n",
       "92830    2100     18727     297\n",
       "92831    2100     18728     281\n",
       "92832    2100     18729     280\n",
       "92833    2100     18730     263\n",
       "\n",
       "[92834 rows x 3 columns]"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_artists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "6e7c199d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>MALICE MIZER</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Diary of Dreams</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Carpathian Forest</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>Moi dix Mois</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>Bella Morte</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17627</th>\n",
       "      <td>18741</td>\n",
       "      <td>Diamanda Galás</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17628</th>\n",
       "      <td>18742</td>\n",
       "      <td>Aya RL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17629</th>\n",
       "      <td>18743</td>\n",
       "      <td>Coptic Rain</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17630</th>\n",
       "      <td>18744</td>\n",
       "      <td>Oz Alchemist</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17631</th>\n",
       "      <td>18745</td>\n",
       "      <td>Grzegorz Tomczak</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>17632 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          id               name\n",
       "0          1       MALICE MIZER\n",
       "1          2    Diary of Dreams\n",
       "2          3  Carpathian Forest\n",
       "3          4       Moi dix Mois\n",
       "4          5        Bella Morte\n",
       "...      ...                ...\n",
       "17627  18741     Diamanda Galás\n",
       "17628  18742             Aya RL\n",
       "17629  18743        Coptic Rain\n",
       "17630  18744       Oz Alchemist\n",
       "17631  18745   Grzegorz Tomczak\n",
       "\n",
       "[17632 rows x 2 columns]"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "artists"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "668d30bf",
   "metadata": {},
   "source": [
    "1.将user_artists和artists两个DataFrame进行合并，以user_artists中的artistID和artists中的id为键值进行连接。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "ce1f0688",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>userID</th>\n",
       "      <th>weight</th>\n",
       "      <th>name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>13883</td>\n",
       "      <td>Duran Duran</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>228</td>\n",
       "      <td>Duran Duran</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>27</td>\n",
       "      <td>85</td>\n",
       "      <td>Duran Duran</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>28</td>\n",
       "      <td>10</td>\n",
       "      <td>Duran Duran</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>62</td>\n",
       "      <td>528</td>\n",
       "      <td>Duran Duran</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92829</th>\n",
       "      <td>2100</td>\n",
       "      <td>337</td>\n",
       "      <td>Nyktalgia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92830</th>\n",
       "      <td>2100</td>\n",
       "      <td>297</td>\n",
       "      <td>Atsakau  niekadA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92831</th>\n",
       "      <td>2100</td>\n",
       "      <td>281</td>\n",
       "      <td>Domantas Razauskas</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92832</th>\n",
       "      <td>2100</td>\n",
       "      <td>280</td>\n",
       "      <td>Atalyja</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92833</th>\n",
       "      <td>2100</td>\n",
       "      <td>263</td>\n",
       "      <td>Les Chants de Nihil</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>92834 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       userID  weight                 name\n",
       "0           2   13883          Duran Duran\n",
       "1           4     228          Duran Duran\n",
       "2          27      85          Duran Duran\n",
       "3          28      10          Duran Duran\n",
       "4          62     528          Duran Duran\n",
       "...       ...     ...                  ...\n",
       "92829    2100     337            Nyktalgia\n",
       "92830    2100     297     Atsakau  niekadA\n",
       "92831    2100     281   Domantas Razauskas\n",
       "92832    2100     280              Atalyja\n",
       "92833    2100     263  Les Chants de Nihil\n",
       "\n",
       "[92834 rows x 3 columns]"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 合并数据集\n",
    "#此处由考生填写(单行)\n",
    "data = pd.merge(user_artists,artists,left_on='artistID',right_on='id')\n",
    "#此处由考生填写(单行)\n",
    "data = data.drop(['id','artistID'],axis=1)# 删除data 中的id 和artistlD列\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e471463",
   "metadata": {},
   "source": [
    "2.去除评分次数少于50次的音乐和用户。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "2de3d7e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2       50\n",
       "1718    50\n",
       "1703    50\n",
       "1699    50\n",
       "1687    50\n",
       "        ..\n",
       "1603     1\n",
       "1731     1\n",
       "1758     1\n",
       "1307     1\n",
       "2085     1\n",
       "Name: userID, Length: 1892, dtype: int64"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "counts = data['userID'].value_counts()#计算每个用户的评分次数\n",
    "counts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "32121f42",
   "metadata": {},
   "outputs": [],
   "source": [
    "#此处由考生填写\n",
    "data = data[data['userID'].isin(counts[counts >= 50].index)] #去除评分次数少于50 次的用户\n",
    "#此处由考生填写\n",
    "counts = data['name'].value_counts() #计算每个音乐的评分次数\n",
    "#此处由考生填写\n",
    "data = data[data['name'].isin(counts[counts >= 50].index)] # 去除评分次数少于50次的音乐\n",
    "#此处由考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "e567f7c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转换成数值才能计算相似度分数\n",
    "# enumerate enumerate是一个内置函数，用于将一个可遍历的数据对象（如列表、元组、字符串等）组合为一个索引序列，同时列出数据下标和数据本身‌\n",
    "#将音乐和用户ID 转换为数字\n",
    "name_to_index={}#创建 name_to_index 字典\n",
    "index_to_name={}#创建 index_to_name 字典\n",
    "for i,name in enumerate(data['name'].unique()): \n",
    "    #遍历data中的 name 列\n",
    "    name_to_index[name]=i#将name 映射到数字\n",
    "    index_to_name[i]=name #将数字映射到 name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0477ce7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"\\nname_to_index：\\n {'Duran Duran': 0,\\n 'Air': 1}\\nindex_to_name:\\n{0: 'Duran Duran',\\n 1: 'Air'}\\n \""
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "name_to_index：\n",
    " {'Duran Duran': 0,\n",
    " 'Air': 1}\n",
    "index_to_name:\n",
    "{0: 'Duran Duran',\n",
    " 1: 'Air'}\n",
    " '''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "783fae2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_id_to_index={}#创建 user_id_to_index 字典\n",
    "index_to_user_id={}#创建 index_to_user_id 字典\n",
    "for i,user_id in enumerate(data['userID'].unique()):\n",
    "    #追历data 中的 userID 列\n",
    "    user_id_to_index[user_id]=i#将user_id 映射到数字\n",
    "    index_to_user_id[i]=user_id #将数字映射到user_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b932add5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nuser_id_to_index:\\n{2: 0,\\n 4: 1}\\nindex_to_user_id:\\n{0: 2,\\n 1: 4,\\n'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "user_id_to_index:\n",
    "{2: 0,\n",
    " 4: 1}\n",
    "index_to_user_id:\n",
    "{0: 2,\n",
    " 1: 4,\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "5aa8d617",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 增加两列\n",
    "data['name_index']= data['name'].apply(lambda x:name_to_index[x])#将data中的name映射到数字\n",
    "data['user_index']= data['userID'].apply(lambda x:user_id_to_index[x])#将data中的userID映射到数字\n",
    "#构建用户-音乐评分矩阵\n",
    "n_users=len(user_id_to_index)#获取用户数量\n",
    "n_artists=len(name_to_index)#获取音乐数量\n",
    "ratings_matrix=np.zeros((n_users,n_artists))#创建用户-音乐评分矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "38866008",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>userID</th>\n",
       "      <th>weight</th>\n",
       "      <th>name</th>\n",
       "      <th>name_index</th>\n",
       "      <th>user_index</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>13883</td>\n",
       "      <td>Duran Duran</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>228</td>\n",
       "      <td>Duran Duran</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>27</td>\n",
       "      <td>85</td>\n",
       "      <td>Duran Duran</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>28</td>\n",
       "      <td>10</td>\n",
       "      <td>Duran Duran</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>62</td>\n",
       "      <td>528</td>\n",
       "      <td>Duran Duran</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   userID  weight         name  name_index  user_index\n",
       "0       2   13883  Duran Duran           0           0\n",
       "1       4     228  Duran Duran           0           1\n",
       "2      27      85  Duran Duran           0           2\n",
       "3      28      10  Duran Duran           0           3\n",
       "4      62     528  Duran Duran           0           4"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bf117f4",
   "metadata": {},
   "source": [
    "3、将 data 中的评分填入用户-音乐评分炬阵，使得ratingsmatrix的输出为:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "f1da945b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#iterrows() : 将DataFrame迭代成（index ,series）\n",
    "#iteritems()：将DataFrame迭代成（列名，series）\n",
    "#itertuples()：将DataFrame迭代成元组 \n",
    "\n",
    "for row in data.itertuples(): # 遍历data\n",
    "    #此处由考生填写\n",
    "    # print(row) \n",
    "    #row打印结果：Pandas(Index=0, userID=2, weight=13883, name='Duran Duran', name_index=0, user_index=0)\n",
    "    # print(row[5]) 打印为0\n",
    "    ratings_matrix[row[5],row[4]] = row[1]#将 data 中的评分填入用户-音乐评分矩阵\n",
    "    #此处由考生填写\n",
    "from sklearn.metrics.pairwise import cosine_similarity #导入 sklearn 库中的余弦相例度计算函敬\n",
    "#计算用户之同的相似度\n",
    "user_similarity=cosine_similarity(ratings_matrix)#计算用户之间的相似度#为用户推荐音乐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "e43ca2da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.,  2.,  2., ...,  0.,  0.,  0.],\n",
       "       [ 4.,  4.,  0., ...,  0.,  0.,  0.],\n",
       "       [27.,  0.,  0., ...,  0.,  0.,  0.],\n",
       "       ...,\n",
       "       [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n",
       "       [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n",
       "       [ 0.,  0.,  0., ...,  0.,  0.,  0.]])"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ratings_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "7920e85e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 0.30618622, 0.1118034 , ..., 0.        , 0.        ,\n",
       "        0.        ],\n",
       "       [0.30618622, 1.        , 0.13693064, ..., 0.        , 0.        ,\n",
       "        0.        ],\n",
       "       [0.1118034 , 0.13693064, 1.        , ..., 0.        , 0.        ,\n",
       "        0.        ],\n",
       "       ...,\n",
       "       [0.        , 0.        , 0.        , ..., 1.        , 0.        ,\n",
       "        0.        ],\n",
       "       [0.        , 0.        , 0.        , ..., 0.        , 1.        ,\n",
       "        0.        ],\n",
       "       [0.        , 0.        , 0.        , ..., 0.        , 0.        ,\n",
       "        1.        ]])"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_similarity"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "466319c1",
   "metadata": {},
   "source": [
    "4.获取与用户最相似的用户索引 similar_users(不包括自己)，使得 similar_users 的输出为:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "805ddf71",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_index=0#设置用户索引\n",
    "#此处由考生填写\n",
    "#np.argsort()是用于获取数组排序后的索引数组。它返回的是数组元素排序后的索引，而不是排序后的数组本身\n",
    "similar_users = np.argsort(-user_similarity[user_index])[1:]#获取与用户索引为0的用户最相似的用户索引\n",
    "#此处由考生填写\n",
    "recommended_artists=[]#创建推荐音乐列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "5c1f5110",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  56,  114,  470, ..., 1418, 1412, 1796], dtype=int64)"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "similar_users"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e3ab0d4",
   "metadata": {},
   "source": [
    "5.填补以下程序，获取相似用户评分的音乐索引artists_rated。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b92b30b",
   "metadata": {},
   "source": [
    "print(np.where(ratings_matrix[i]>0))结果如下：\n",
    "(array([  0,   2,   3,   4,   5,   8,   9,  11,  20,  24,  74, 166, 239,\n",
    "       253], dtype=int64),)\n",
    "(array([  1,   2,   4,   6,   7,   8,   9,  10,  11,  12,  14,  15,  18,\n",
    "        24,  56,  61,  65,  68,  69,  70,  73,  80, 110, 112, 151, 153,\n",
    "       162, 163, 181, 184, 220, 238, 252, 278, 306, 313], dtype=int64),)\n",
    "(array([  3,   8,   9,  11,  12,  56, 253, 254], dtype=int64),)\n",
    "...\n",
    "\n",
    "\n",
    "print(np.where(ratings_matrix[i]>0)[0])结果如下：\n",
    "[  0   2   3   4   5   8   9  11  20  24  74 166 239 253]\n",
    "[  1   2   4   6   7   8   9  10  11  12  14  15  18  24  56  61  65  68\n",
    "  69  70  73  80 110 112 151 153 162 163 181 184 220 238 252 278 306 313]\n",
    "[  3   8   9  11  12  56 253 254]\n",
    "..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "422f0538",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Slayer\n",
      "Yann Tiersen\n",
      "Boards of Canada\n",
      "Radiohead\n",
      "The Cure\n",
      "Pink Floyd\n",
      "The Cranberries\n",
      "Arcade Fire\n",
      "Blur\n",
      "Beck\n"
     ]
    }
   ],
   "source": [
    "for i in similar_users:#遍历相似用户索引\n",
    "    #此处由考生填写\n",
    "    #np.where()是一个强大的函数，它根据条件对数组元素进行选择或替换\n",
    "    # 默认情况下，np.where(条件)返回满足条件的元素值。如果想要返回满足条件的元素的索引，可直接对条件表达式使用np.where()。\n",
    "    artists_rated = np.where(ratings_matrix[i]>0)[0]# 获取相似用户评分的音乐索引\n",
    "    #此处由考生填写\n",
    "    for j in artists_rated:#遍历音乐索引\n",
    "        if ratings_matrix[user_index][j] == 0:#用户未评分\n",
    "            recommended_artists.append((j,ratings_matrix[i][j]))# 将音乐加入推荐音乐列表\n",
    "recommended_artists = sorted(recommended_artists, key=lambda x: x[1],reverse=True)[:10]#筛选出推荐音乐中的前10首\n",
    "for artist in recommended_artists:#遍历推荐音乐\n",
    "    print(artists[artists['name']== index_to_name[artist [0]]]['name'].values[0])\n",
    "#打印推荐音乐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b52fc39e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
